package com.example.dd.androiddemo_navigationview;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main);
        NavigationView mNavigationView = (NavigationView) findViewById(R.id.design_navigation_view);



        mToolbar.setTitle("Android Demo");
        setSupportActionBar(mToolbar);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {   //  必须在setSupportActionBar之后才有作用
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.item4:
                        Toast.makeText(MainActivity.this, "哈哈", Toast.LENGTH_SHORT).show();
                        Snackbar.make(mToolbar, "哈哈", Snackbar.LENGTH_LONG).setAction("jump", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(MainActivity.this, CoordinatorlayoutActivity.class));
                            }
                        }).show();
                        break;
                    case R.id.item5:
                        Toast.makeText(MainActivity.this, "嘿嘿", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this, BottomActivity.class));
                        break;
                    case R.id.item6:
                        Toast.makeText(MainActivity.this, "嘻嘻", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this, TablayoutActivity.class));
                        break;
                }
                return true;
            }
        });



        //设置抽屉DrawerLayout
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
                R.string.drawer_open, R.string.drawer_close);
        mDrawerToggle.syncState();//初始化状态
        mDrawerLayout.addDrawerListener(mDrawerToggle);
    //设置导航栏NavigationView的点击事件
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                mDrawerLayout.closeDrawers();
                int itemId = item.getItemId();
                switch (itemId){
                    case R.id.item1:
                        Toast.makeText(MainActivity.this, "哈哈", Toast.LENGTH_SHORT).show();
                        Snackbar.make(mToolbar, "哈哈", Snackbar.LENGTH_LONG).setAction("jump", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(MainActivity.this, CoordinatorlayoutActivity.class));
                            }
                        }).show();

                        break;
                    case R.id.item2:
                        Toast.makeText(MainActivity.this, "嘿嘿", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this, BottomActivity.class));
                        break;
                    case R.id.item3:
                        Toast.makeText(MainActivity.this, "嘻嘻", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this, TablayoutActivity.class));
                        break;
                }
                return false;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_right,menu);
        return super.onCreateOptionsMenu(menu);
    }
}
